#!/usr/bin/python3
import main
import time
from threading import Timer
from flask import Flask, request
import Pyconfig
import service.MainService
import DB
import json
import sys
import TimerStart

# Flask初始化参数尽量使用你的包名，这个初始化方式是官方推荐的，官方解释：http://flask.pocoo.org/docs/0.12/api/#flask.Flask
app = Flask(__name__)


@app.route('/sendsms', methods=['POST'])
def sendsms():
    jsonv = json.loads(request.data)
    uuid = jsonv["uuid"]
    msgtitle = jsonv['msgtitle']
    msgcontent = jsonv['msgcontent']
    except_send = jsonv['except_send']
    recover_send = jsonv['recover_send']
    except_type = jsonv['except_type']
    try:
        conn_out = DB.conn_out
        MainService_o = service.MainService.MainService(conn_out)
        rows3 = MainService_o.selectxx_1()
        if (len(rows3) < 1):
            idv = 1
        else:
            idv = rows3[0][0] + 1
        returst1 = MainService_o.insterxx_1(idv, uuid, msgtitle, msgcontent, except_send, recover_send, except_type)
        if (returst1):
            returnstr = "{'succ': True, 'code': 200, 'msg': '', 'data': '成功'}"
            return returnstr
        else:
            returnstr = "{'succ': False, 'code': -1, 'msg': '', 'data': '失败'}"
            return returnstr
    except:
        returnstr = "{'succ': False, 'code': -1, 'msg': '', 'data': '失败'}"
        return returnstr


@app.route('/sendSmsToOSC', methods=['GET', 'POST'])
def sendSmsToOSC():
    print(request)
    jsonv = json.loads(request.data)
    title = jsonv["title"]
    msgtitle = jsonv["msg"]
    returst1 = main.sendSmsToOSC(title, msgtitle)
    if (returst1):
        returnstr = "{'succ': True, 'code': 200, 'msg': '', 'data': '成功'}"
        return returnstr
    else:
        returnstr = "{'succ': False, 'code': -1, 'msg': '', 'data': '失败'}"
        return returnstr

@app.route('/testnew', methods=['GET', 'POST'])
def testnew():
    TimerStart.startgo();

if __name__ == "__main__":
    # 这种是不太推荐的启动方式，我这只是做演示用，官方启动方式参见：http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application
    app.run(host="0.0.0.0", port=8977, debug=True)
